home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 685 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.4 KB  |  76 lines

  1. Path: solon.com!not-for-mail
  2. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: auto_ptr: no operator bool()?
  5. Date: 11 Mar 1996 19:25:47 -0600
  6. Organization: Digital Solutions
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4i2jqr$8dl@solutions.solon.com>
  10. References: <313ddfd9.16044605@sqarc.sq.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <313ddfd9.16044605@sqarc.sq.com> willer@carolian.com (Steve
  14. Willer) writes:
  15.  
  16. |> I hope I don't sound like I'm second-guessing people who are likely much more
  17. |> experienced than I am in library design, but I have another question about
  18. |> auto_ptrs.
  19.  
  20. |> Specifically, the (April '95) draft standard auto_ptr doesn't have an operator
  21. |> bool() function in it, making it inconvenient to write "if (ptr)" type
  22. |> constructs. Is there some sort of reason for that? I know that implicit type
  23. |> conversion is generally a bad thing, but I can't see how this one would be
  24. |> particularly dangerous (also, can the new explicit keyword be used on the
  25. |> return type?).
  26.  
  27. The real danger is in the following:
  28.  
  29.     auto_ptr< T >    p1 ;
  30.     auto_ptr< T >    p2 ;
  31.  
  32.     if ( p1 == p2 ) ...
  33.  
  34. You thought you were comparing two pointers, but in fact, you are
  35. comparing the results of comparing these pointers to null.  The
  36. expression in the if (illegal with the current definition) will cause
  37. the conversion operator bool to be called for both pointers.
  38.  
  39. |> Now, Meyers's latest book describes an auto_ptr that has member templates, so
  40. |> obviously the April WP does not have the latest auto_ptr, but I get the
  41. |> impression even the current auto_ptr doesn't have an operator bool(). My
  42. |> reading of Meyers's discussion of smart_ptrs mentions that operator bool()
  43. |> would allow comparison of two different types of auto_ptrs, but the latest
  44. |> iostream standard has an operator bool() and an operator!() defined. So why not
  45. |> be consistent?
  46.  
  47. I agree.  But the better solution would be to remove the operators
  48. from iostream:-).
  49.  
  50. More likely, the problem is that an auto_ptr looks very much like a
  51. regular pointer, and people often compare pointers.  How often have
  52. you wanted to actually compare two streams:-)?
  53.  
  54. |> In fact, as another idea, what about defining a new class, say, explicit_bool?
  55. |> explicit_bool would have an operator bool() defined, but would also have an
  56. |> operator==(const explicit_bool &rhs) and operator!=(const explicit_bool &rhs)
  57. |> defined as a private function. It would also have operator!() return an
  58. |> explicit_bool. This way, constructs such as "if (myptr)" and "if (!myptr)"
  59. |> would work, but "if (myptr==otherptr)" and "if (myptr != otherptr)" wouldn't
  60. |> compile.
  61.  
  62. I'm not sure what this would buy us.  The `obvious' solution for
  63. auto_ptr is to add an `isValid' function; in the meantime, just use
  64. `if ( ptr.get() == NULL )'.  (Another possible alternative is to
  65. provide a conversion operator to the underlying pointer type, so that
  66. you can write `if ( ptr == NULL )', just as you would with a normal
  67. pointer.  I personally don't like the fact that this might result in
  68. free pointers to the memory without having called a function
  69. explicitly.) 
  70.  
  71. --
  72. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  73. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  74. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  75.                 -- A la recherche d'une activitΘ dans une region francophone
  76.